<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>数组去重</title>
</head>
<body>
  <script>
    // 方法一：
    // 先将原数组排序，在与相邻的进行比较，如果不同则存入新数组
    function unique(arr) {
      var arr2 = arr.sort();
      var res = [arr2[0]];

      for(var i = 1; i<arr2.length; i++) {
        if(arr2[i] !== res[res.length-1]){
          res.push(arr2[i]);
        }
      }
      return res;
    }

    const num = [1,3,2,5,44,22,1,3,2];
    var m = unique(num);
    console.log(m);


    // 利用下标查询：
    function Twounique(arr) {
      var newArr = [arr[0]];
      for(var i=1; i<arr.length; i++) {
        if(newArr.indexOf(arr[i]) == -1) {
          newArr.push(arr[i]);
        }
      }
      return newArr;
    }
    Twounique(num);
    console.log(m);
  
  </script>
</body>
</html>